package com.nantian.nfcm.bms.loan.web;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nantian.nfcm.bms.loan.service.LoanApplicationQueryService;
import com.nantian.nfcm.bms.loan.service.LoanApplicationService;
import com.nantian.nfcm.bms.loan.vo.LoanBean;
import com.nantian.nfcm.util.vo.GridData;
import com.nantian.nfcm.util.vo.LoginBean;
import com.nantian.nfcm.util.vo.ResultInfo;

@Controller
@RequestMapping("/loan")
public class LoanApplicationController {
    private static Logger log = LoggerFactory.getLogger(LoanApplicationController.class);
    private LoanApplicationService loanApplicationService;
    private LoanApplicationQueryService loanApplicationQueryService;
    

    public LoanApplicationController(LoanApplicationService loanApplicationService, 
    		LoanApplicationQueryService loanApplicationQueryService) {
    	this.loanApplicationQueryService = loanApplicationQueryService;
        this.loanApplicationService = loanApplicationService;
    }

    /**
     * 新增贷款信息
     * @param request
     * @param loanBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/addLoan")
    @ResponseBody
    private ResultInfo addLoan(HttpServletRequest request, LoanBean loanBean) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
        	LoginBean loginUser = (LoginBean)request.getSession().getAttribute("loginInfo");
            LoanBean loan = loanApplicationService.addLoanApplication(loanBean, loginUser);
            resultInfo.setSuccess("true");
            resultInfo.setData(loan);
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }

    /**
     * 修改贷款信息
     * @param request
     * @param loanBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateLoan")
    @ResponseBody
    private ResultInfo updateLoan(HttpServletRequest request, LoanBean loanBean) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
        	LoginBean loginUser = (LoginBean)request.getSession().getAttribute("loginInfo");
            LoanBean loan = loanApplicationService.updateLoanApplication(loanBean, loginUser);
            resultInfo.setSuccess("true");
            resultInfo.setData(loan);
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }

    /**
     * 根据ID查询贷款信息
     * @param loanBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/findById")
    @ResponseBody
    private ResultInfo findById(LoanBean loanBean) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
            LoanBean loan = loanApplicationQueryService.findById(loanBean.getLoanId());
            resultInfo.setSuccess("true");
            resultInfo.setData(loan);
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }
    
    /**
     * 根据条件查询所有贷款记录，提供给分配人和上报人使用的全局查询
     * @param request
     * @param page
     * @param size
     * @param loanJournalBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/assignerfindByCondition")
    @ResponseBody
    private ResultInfo findAllLoanByCondition(int page, int size, LoanBean loanBean) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
            GridData<LoanBean> gd = loanApplicationQueryService.findAllLoanByCondition(page, size, loanBean);
            resultInfo.setData(gd.getData());
            resultInfo.setNumber(gd.getNumber());
            resultInfo.setPage(gd.getPage());
            resultInfo.setTotalPage(gd.getTotalPage());
            resultInfo.setSuccess("true");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }
    
    /**
     * 用于记录分配人已经收到纸质资料
     * @param request
     * @param page
     * @param size
     * @param loanJournalBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/assignerReceivedFiles")
    @ResponseBody
    private ResultInfo assignerReceivedFiles(HttpServletRequest request, LoanBean loanBean) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
        	LoginBean loginUser = (LoginBean)request.getSession().getAttribute("loginInfo");
            loanApplicationService.assignerReceivedFiles(loanBean, loginUser);
            resultInfo.setSuccess("true");
            resultInfo.setData("");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }
    
    /**
     * 贷款信息条件查询
     * @param request
     * @param page
     * @param size
     * @param loanJournalBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/findByCondition")
    @ResponseBody
    private ResultInfo findByCondition(HttpServletRequest request, int page, int size, LoanBean loanJournalBean) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
        	LoginBean loginUser = (LoginBean)request.getSession().getAttribute("loginInfo");
            GridData<LoanBean> gd = loanApplicationQueryService.findByCondition(page, size, loanJournalBean,loginUser);
            resultInfo.setData(gd.getData());
            resultInfo.setNumber(gd.getNumber());
            resultInfo.setPage(gd.getPage());
            resultInfo.setTotalPage(gd.getTotalPage());
            resultInfo.setSuccess("true");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }
    
    /**
     * 查询需要记录审批结果的贷款
     * @param request
     * @param page
     * @param size
     * @param loanBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/findByNeedApprove")
    @ResponseBody
    private ResultInfo findByNeedApprove(HttpServletRequest request, int page, int size, LoanBean loanBean) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
        	LoginBean loginUser = (LoginBean)request.getSession().getAttribute("loginInfo");
            GridData<LoanBean> gd = loanApplicationQueryService.findByNeedApprove(page, size, loanBean, loginUser);
            resultInfo.setData(gd.getData());
            resultInfo.setNumber(gd.getNumber());
            resultInfo.setPage(gd.getPage());
            resultInfo.setTotalPage(gd.getTotalPage());
            resultInfo.setSuccess("true");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }

    /**
     * 贷款上报
     * @param loanBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/loanSubmit")
    @ResponseBody
    private ResultInfo updateLoanJournalDistribution(HttpServletRequest request, LoanBean loanBean) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
        	LoginBean loginUser = (LoginBean)request.getSession().getAttribute("loginInfo");
        	loanApplicationService.loanSubmit(loanBean, loginUser);
            resultInfo.setSuccess("true");
            resultInfo.setData("");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }
    
    /**
     * 分配贷款独立审查人
     * @param request
     * @param loanBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/loanAssigned")
    @ResponseBody
    private ResultInfo loanAssigned(HttpServletRequest request, LoanBean loanBean) throws Exception {
    	ResultInfo resultInfo = new ResultInfo();
        try {
        	LoginBean loginUser = (LoginBean)request.getSession().getAttribute("loginInfo");
        	loanApplicationService.loanAssigned(loanBean, loginUser);
            resultInfo.setSuccess("true");
            resultInfo.setData("");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }

    /**
     * 贷款审查成功请求
     * @param request
     * @param loanBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/loanReviewSuccess")
    @ResponseBody
    private ResultInfo loanReviewSuccess(HttpServletRequest request, LoanBean loanBean) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
        	LoginBean loginUser = (LoginBean)request.getSession().getAttribute("loginInfo");
        	loanApplicationService.loanReviewSuccess(loanBean, loginUser);
            resultInfo.setSuccess("true");
            resultInfo.setData("");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }
    
    /**
     * 客户经理执行补件
     * @param request
     * @param loanBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/loanResubmitComplete")
    @ResponseBody
    private ResultInfo loanResubmitComplete(HttpServletRequest request, LoanBean loanBean) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
        	LoginBean loginUser = (LoginBean)request.getSession().getAttribute("loginInfo");
        	loanApplicationService.loanResubmitComplete(loanBean, loginUser);
            resultInfo.setSuccess("true");
            resultInfo.setData("");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }

    /**
     * 工作转交
     * @param request
     * @param loanBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/loanWorkTransfer")
    @ResponseBody
    private ResultInfo loanWorkTransfer(HttpServletRequest request, LoanBean loanBean) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
        	LoginBean loginUser = (LoginBean)request.getSession().getAttribute("loginInfo");
        	loanApplicationService.workTransfer(loanBean, loginUser);
            resultInfo.setSuccess("true");
            resultInfo.setData("");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }
    
    /**
     * 要求补件
     * @param request
     * @param loanBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/loanJournalResubmit")
    @ResponseBody
    private ResultInfo updateLoanJournalResubmit(HttpServletRequest request, LoanBean loanBean) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
        	LoginBean loginUser = (LoginBean)request.getSession().getAttribute("loginInfo");
        	loanApplicationService.updateLoanJournalResubmit(loanBean, loginUser);
            resultInfo.setSuccess("true");
            resultInfo.setData("");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }

    /**
     * 退回给分配人
     * @param request
     * @param loanBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/backToAssigner")
    @ResponseBody
    private ResultInfo backToAssigner(HttpServletRequest request, LoanBean loanBean) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
        	LoginBean loginUser = (LoginBean)request.getSession().getAttribute("loginInfo");
        	loanApplicationService.backToAssigner(loanBean, loginUser);
            resultInfo.setSuccess("true");
            resultInfo.setData("");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }
    
    /**
     * 退件
     * @param request
     * @param loanBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/loanRefuse")
    @ResponseBody
    private ResultInfo updateLoanJournalRefuse(HttpServletRequest request, LoanBean loanBean) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
        	LoginBean loginUser = (LoginBean)request.getSession().getAttribute("loginInfo");
        	loanApplicationService.updateLoanJournalRefuse(loanBean, loginUser);
            resultInfo.setSuccess("true");
            resultInfo.setData("");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }

    /**
     * 记录审批结果为成功
     * @param request
     * @param loanBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/loanApprovalSucc")
    @ResponseBody
    private ResultInfo loanApprovalSucc(HttpServletRequest request, LoanBean loanBean) throws Exception {
        return loanApproval(request, loanBean, true);
    }
    
    /**
     * 直接上报区联社
     * @param request
     * @param loanBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/reportToParent")
    @ResponseBody
    private ResultInfo reportToParent(HttpServletRequest request, LoanBean loanBean) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
        	LoginBean loginUser = (LoginBean)request.getSession().getAttribute("loginInfo");
        	loanApplicationService.reportToParent(loanBean, loginUser);
            resultInfo.setSuccess("true");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        
        return resultInfo;
    }
    
    /**
     * 记录审批结果为失败
     * @param request
     * @param loanBean
     * @return
     * @throws Exception
     */
    @RequestMapping("/loanApprovalFail")
    @ResponseBody
    private ResultInfo loanApprovalFail(HttpServletRequest request, LoanBean loanBean) throws Exception {
        return loanApproval(request, loanBean, false);
    }
    
    /**
     * 记录审批状态的公共方法
     * @param request
     * @param loanBean
     * @param result
     * @return
     * @throws Exception
     */
    private ResultInfo loanApproval(HttpServletRequest request, LoanBean loanBean, boolean result) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
        	LoginBean loginUser = (LoginBean)request.getSession().getAttribute("loginInfo");
        	loanApplicationService.updateLoanJournal2Review(loanBean, loginUser, result);
            resultInfo.setSuccess("true");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }
    
}
